package com.dgut.hisystem.controller;

import com.dgut.hisystem.entity.Case;
import com.dgut.hisystem.entity.Drug;
import com.dgut.hisystem.entity.Register;
import com.dgut.hisystem.entity.VO.AdviceVO;
import com.dgut.hisystem.entity.VO.CaseVO;
import com.dgut.hisystem.service.CaseService;
import com.dgut.hisystem.service.DrugService;
import com.dgut.hisystem.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/case")
public class CaseController {
    @Autowired
    private CaseService caseService;
    @Autowired
    private RegisterService registerService;
    @Autowired
    private DrugService drugService;
    @RequestMapping("/select")
    Case select(@RequestParam("id") long id){
        return caseService.selectID(id);
    }
    @RequestMapping("selectAdviceVO")
    List<AdviceVO> selectAdviceVO(@RequestParam("bl_id") long bl_id){
        return caseService.selectAdviceVO(bl_id);
    }
    @RequestMapping("/selectPending")
    List<CaseVO> selectPending(HttpServletRequest request){
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return caseService.selectPending();
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return caseService.selectPendingByCard(card);
        }
    }
    @RequestMapping("/passPending")
    void passPending(@RequestParam("id") long id){
        Case case1 = new Case();
        Register register = registerService.getRegisterById(id);
        case1.setBr_card(register.getBr_card());
        case1.setYs_id(register.getYs_id());
        case1.setBl_consultation(0);
        registerService.delete(id);
        caseService.insert(case1);
    }
    @RequestMapping("/selectRecord")
    List<CaseVO> selectRecord(HttpServletRequest request){
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return caseService.selectRecord();
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return caseService.selectRecordByCard(card);
        }
    }

    @RequestMapping("/selectTreat")
    List<CaseVO> selectTreat(HttpServletRequest request){
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return caseService.selectTreat();
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return caseService.selectTreatByCard(card);
        }
    }

    @RequestMapping("/selectDrug")
    List<Drug> selectDrug(HttpServletRequest request){
        if(request.getParameter("id") == null|| request.getParameter("id").equals("")){
            return drugService.getAll();
        }
        else{
            long id = Long.parseLong(request.getParameter("id"));
            return drugService.getDrug(id);
        }
    }

    @RequestMapping("/update")
    void update(@RequestBody Case case1,@RequestParam("bl_id") long id){
        case1.setBl_consultation(1);
        caseService.update(case1,id);
    }

    @RequestMapping("/insertPrescription")
    void insertPrescription(@RequestBody List<AdviceVO> adviceVOS,@RequestParam("bl_id") long id){
        caseService.insertPrescription(adviceVOS, id);
    }
}
